package com.personal.website.lottery.table

import java.util
import scala.collection.JavaConverters._

/**
 * Created by Gavin on 2015/12/13.
 */
class End3dTable(issue: Int,redBalls: Seq[Int], blueBall: Int) extends Table(issue,redBalls,blueBall){
  override def createRecord() = createBasicBalls() ++ createEnd012Balls() ++ createEnd012Vs() ++ createEnd012Miss() ++ createMainRecord()
  def createBasicBalls() = redBalls.sorted
  def createEnd012Balls() = redBalls.map(_ % 3)
  def createEnd012Vs() = (0 to 2).map(index => createEnd012Balls().count(_.equals(index)))
  def createEnd012Miss() = {
    val leftHalf = redBalls.take(redBalls.length / 2)
    val rightHalf = redBalls.takeRight(redBalls.length / 2)
    (0 to 2).map(index => if(leftHalf.contains(index)) index else DEFAULT_NON_EXISTS_VALUE) ++
    (0 to 2).map(index => if(rightHalf.contains(index)) index else DEFAULT_NON_EXISTS_VALUE)
  }
  def createMainRecord() = End3dTable.ALL_EXISTS_BALLS.map(ball => if(redBalls.contains(ball)) ball else DEFAULT_NON_EXISTS_VALUE)

  override def toString() = Seq(Seq(issue),createRecord()).flatten.mkString(";")
}

object End3dTable extends {
  def apply(issue: Int,redBalls: Seq[Int], blueBall: Int) = new End3dTable(issue,redBalls,blueBall)
  def apply(issue: Int,allBalls: Seq[Int]) = new End3dTable(issue,allBalls.init,allBalls.last)
  def newInstance(issue: Integer,redBalls: util.List[Integer],blueBall: Integer) = new End3dTable(issue.toInt, redBalls.asScala.map(_.toInt),blueBall.toInt)

  val ALL_EXISTS_BALLS = (1 to 33).filter(toEnd(_) % 3 == 0) ++
    (1 to 33).filter(toEnd(_) % 3 == 1) ++
    (1 to 33).filter(toEnd(_) % 3 == 2)

  def toEnd(num: Int) = (num - 1) % 10 + 1

  val headString = Seq("basic balls","end012","end012vs","end012miss",ALL_EXISTS_BALLS).mkString("|")
}
